<template>
  <div class="app-container customer-div">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="客户编号：">
          <el-input v-model="listQuery.customerNumber"></el-input>
<!--          <el-autocomplete-->
<!--            clearable-->
<!--            class="inline-input"-->
<!--            value-key="customerName"-->
<!--            :fetch-suggestions="querySearch"-->
<!--            v-model="listQuery.customerName"-->
<!--            placeholder="请输入内容"-->
<!--            @select="handleSelectCustomerName"-->
<!--            :trigger-on-focus="false"-->
<!--          ></el-autocomplete>-->
        </el-form-item>
        <el-form-item label="跟进内容：">
           <el-input v-model="listQuery.trackingContent"></el-input>
        </el-form-item>
        <el-form-item label="跟进方式：">
          <el-select  v-model="listQuery.trackingTypeId" placeholder="请选择" clearable>
            <el-option v-for="item in trackingMethodList"  :value="item.tmId" :label="item.trackingMethod" :key="item.trackingMethod"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-date-picker
            v-model="listQuery.createTime"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="沟通方式：">
          <el-select  v-model="listQuery.trackingMethodId" placeholder="请选择" clearable>
            <el-option v-for="item in interactList"  :value="item.interactId" :label="item.interactType" :key="item.interactId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进目的：">
          <el-select  v-model="listQuery.trackingPurposeId" placeholder="请选择" clearable>
            <el-option v-for="item in purposeList"  :value="item.purposeId" :label="item.purposeTitle" :key="item.purposeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务员：" v-if="listQuery.type == 'all'">
          <el-select v-model="listQuery.userId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
<!--          <el-button-->
<!--            @click="showGj(null,'add')"-->
<!--            type="primary">-->
<!--            新增跟进-->
<!--          </el-button>-->
        </el-form-item>
      </el-form>

    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column  label="客户编号" min-width="200"  header-align="center">
          <template slot-scope="scope">
              <el-link @click="toDetail(scope.row)" type="primary">
                {{scope.row.customerNumber}}
              </el-link>
            <!--是否有权限-->
            <div v-if="scope.row.isShowCustomerName" style="white-space: normal;">{{scope.row.customerName}}</div>
          </template>
        </el-table-column>

        <el-table-column
          label="产品线"
          align="center"
          min-width="150">
          <template slot-scope="scope">
              {{scope.row.goodsLineNames}}
          </template>
        </el-table-column>
        <el-table-column
          label="客户联系人"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.contactName}}
          </template>
        </el-table-column>
        <el-table-column
          label="开发阶段 "
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.stepName}}
          </template>
        </el-table-column>

        <el-table-column
          label="跟进人"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column
          label="跟进方式"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.trackingTypeValue}}
          </template>
        </el-table-column>
        <el-table-column
          label="沟通方式"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.trackingMethodValue}}
          </template>
        </el-table-column>
        <el-table-column
          label="跟进目的"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.trackingPurposeValue}}
          </template>
        </el-table-column>
        <el-table-column
          label="跟进内容"
          min-width="200"
          header-align="center">
          <template slot-scope="scope">
            {{scope.row.trackingContent}}
          </template>
        </el-table-column>
        <el-table-column
          label="附件"
          min-width="250"
          align="center">
          <template slot-scope="{row}">
            <!-- <el-image
              v-for="item in scope.row.trackingPics"
              :key="item"
              style="width: 50px; height: 50px"
              :src="item"
              :preview-src-list="scope.row.trackingPics">
            </el-image> -->
            <div v-if="row.trackingPics.length>0">
            <div v-for="(item,index) in row.trackingPics" :key="index" :class="{imgStyle:checkFileType(item)=='image'}">
              <!-- 音频 -->
              <div v-if="checkFileType(item)=='audio'">
                <audio controls style="width: 220px; height: 30px">
                  <source :src="item" type="audio/mpeg" />
                  <source :src="item" type="audio/ogg" />
                  <source :src="item" type="audio/mp3" />
                  您的浏览器不支持该音频格式。
                </audio>
              </div>
              <!-- 图片 -->
              <div v-else-if="checkFileType(item)=='image'" style="width: 40px;margin-right:10px;">
                <el-image
                  style="width: 40px; height: 40px;"
                  :src="item"
                  :preview-src-list="Array(item)">
                </el-image>
              </div>
              <div v-else>
                <div>{{ item }}</div>
              </div>
            </div>
          </div>
          </template>
        </el-table-column>
        <el-table-column
          label="下次跟进时间"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.nextTrackingTime}}
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.trackingRemark}}
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.createTime}}
          </template>
        </el-table-column>

        <el-table-column
          width="100"
          fixed="right"
          label="操作"
          align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="showGj(scope.row,'edit')">编辑
            </el-button>
            <!-- <el-button
              type="text"
              size="mini"
              @click="delItme(scope.row)">删除
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

    <!--编辑跟进-->
    <addGj ref="addGj" @getList="getList"></addGj>
  </div>
</template>
<script>
import {getCustomerOption,getOrderUsersList,} from '@/api/partner'
import {formatDate} from '@/utils/date';
import { getPurposeListAPI,getTrackingMethodListAPI,getInteractListAPI } from "@/api/fms.js";
import {customerTrackingList,delTracking} from '@/api/company'
import {validateCommission} from '@/utils/validate';
import store from '@/store'
import "@/utils/jQuery.print"
import { mapGetters } from 'vuex'
import addGj  from './addGj'
const defaultListQuery={
  pageNum: 1,
  pageSize: 10,
  trackingContent:'',
  trackingTypeId:'',
  trackingMethodId:'',
  trackingPurposeId:'',
  createTime:[],
  type:'',
  customerName:'',
  userId:null,
  customerNumber:''
};
const defaultSendForm={
  deliverId: '',
  goodsReceipt: [],
  goodsReceiptRemark: '',
  goodsReceiptDate:''
};
export default {
  name: "followCalendar",
  data(){


    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      roles:store.getters.roles ? store.getters.roles : [],

      trackingMethodList:[],
      interactList:[],
      purposeList:[],
      userOptionList:[]

    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
  },
  created() {

  },
  filters: {
    formatCreateTime(time) {
      if(time){
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd')
      }else{
        return '--'
      }
    },
  },
  components : {
    addGj
  },
  methods: {
    openWord(item) {
      window.open(item)
    },
     // 判断文件类型
    checkFileType(fileName) {
      if (fileName.match(/\.(jpg|jpeg|png|gif|webp)/)) {
        return "image";
      } else if (fileName.match(/\.(doc|docx)/)) {
        return "word";
      } else if (fileName.match(/\.(wav|mp3|aac|ogg|wma)/)) {
        return "audio";
      } else {
        return "unknown";
      }
    },
    // delItme(item){
    //   this.$confirm('确认删除吗？', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     delTracking({ trackingId: item.trackingId }).then((res) => {
    //       this.$message({
    //         type: 'success',
    //         message: '已删除成功!'
    //       });
    //       this.getList();
    //     })
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '已取消删除'
    //     });
    //   });
    // },
    //获取员工下拉选项
    getUserOptionListData(){
      getOrderUsersList().then((res)=>{
        this.userOptionList = res;
      })
    },
    //跟进目的列表
    getPurposeList(){
      getPurposeListAPI({pageNum:1,pageSize: 1000,isEnable:1}).then((res)=>{
        this.purposeList = res.list
      })
    },

    //跟进方式
    getTrackingMethodList(){
      getTrackingMethodListAPI({pageNum:1,pageSize: 1000,isEnable:1}).then((res)=>{
        this.trackingMethodList = res.list
      })
    },

    //沟通方式
    getInteractList(){
      getInteractListAPI({pageNum:1,pageSize: 1000,isEnable:1}).then((res)=>{
        this.interactList = res.list
      })
    },

    init(type){
      this.listQuery.type = type
      this.getList();
      this.getUserOptionListData();
      this.getPurposeList();
      this.getTrackingMethodList();
      this.getInteractList()
    },
    //新增编辑跟进
    showGj(item,type){
      // let data = {}
      // data.customerId = item.customerId
      // data.customerName = item.customerName
      this.$refs.addGj.showTracking(item,type,this.listQuery.type)
    },

    //去客户详情页
    toDetail(item) {
      let routeData = this.$router.resolve({name:'customerDetail',query:{id:item.customerId}});
      window.open(routeData.href);
    },

    //客户名称输入匹配
    querySearch(queryString, cb){
      getCustomerOption({keywords:queryString}).then((res)=>{
        cb(res);
      })
    },


    //选择客户名称
    handleSelectCustomerName(item){
      this.listQuery.customerName = item.customerName
    },


    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取列表
    getList() {
      this.listLoading = true;
      this.tableData = [];
      customerTrackingList(this.listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.listLoading = false;
      }).catch(()=>{
        this.listLoading = false;
      });
    },






  },
}
</script>
<style scoped>
.imgStyle{
  float: left;
}
</style>



